#!/bin/bash

# Bash file to run test ARQ 
# @author rouil/sharat

RES_DIR=res_test_arq #output directory

if [ "$1" == "clean" ]; then
    rm -r $RES_DIR
else
    mkdir $RES_DIR
    cd $RES_DIR

    for loss in "0" "0.1" "0.15" "0.2" "0.25" "0.3" "0.35" "0.4"; do		
	mkdir "Loss"$loss
	cd "Loss"$loss
	ln -s ../../../../PED_A
	ln -s ../../../../BLER_LookupTable.txt
	ln -s ../../../../BetaTable.txt
	echo -n "Running for " $loss " data loss..."
	# first test without arq
	echo -n "no ARQ"
	ns ../../test-arq-ul.tcl $loss 0 &> log.t
	SENT=`grep AGT out.res | grep cbr | grep -c ^s`
	RECV0=`grep "1 0 cbr" out.res | grep -c ^r `
	LOSS0=`echo $SENT $RECV0 | awk '{print (1-($2/$1))}'`
	DATARATE0=`grep ^r out.res|grep "1 0 cbr"| awk 'BEGIN{first=-1; last=-1} {if (first==-1) {first=$2}; last=$2; i+=$6-0;} END {print (8*i/(last-first))}'`
	DELAY0=`grep delay log.t | grep "Mac 1" | awk '{d+=$9; i++}END{print (d/i)}'`

	# second test with arq
	echo -n ", ARQ:"
	ns ../../test-arq-ul.tcl $loss 1 &> log.t
	RECV1=`grep "1 0 cbr" out.res | grep -c ^r `
	LOSS1=`echo $SENT $RECV1 | awk '{print (1-($2/$1))}'`
	DATARATE1=`grep ^r out.res|grep "1 0 cbr"| awk 'BEGIN{first=-1; last=-1} {if (first==-1) {first=$2}; last=$2; i+=$6-0;} END {print (8*i/(last-first))}'`
	DELAY1=`grep delay log.t | grep "Mac 1" | awk '{d+=$9; i++}END{print (d/i)}'`

	echo $loss $SENT $RECV0 $RECV1 $LOSS0 $LOSS1 $DATARATE0 $DATARATE1 $DELAY0 $DELAY1
	echo $loss $SENT $RECV0 $RECV1 $LOSS0 $LOSS1 $DATARATE0 $DATARATE1 $DELAY0 $DELAY1 >>../results.dat
	rm out.res
	rm log.t
	cd ..
    done
    cd ..
    gnuplot plot-test-arq-ul	
    evince test-arq-delay-ul.eps
    evince test-arq-datarate-ul.eps
    evince test-arq-loss-ul.eps
fi
